package com.wanban.adutils.advertisers.admob

import android.app.Activity
import android.util.Log
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
import com.wanban.adutils.AdUtils
import com.wanban.adutils.advertisers.BaseAdLoader
import com.wanban.adutils.db.bean.AdInfo
import com.wanban.adutils.callback.OnLoadAdCallback
import com.wanban.adutils.tools.AdConstants

class AdmobRewardedAdUtils : BaseAdLoader() {

    private var rewardedAd: RewardedAd? = null


    override fun loadAdNew(activity: Activity, adInfo: AdInfo, callback: OnLoadAdCallback) {
        super.loadAdNew(activity, adInfo, callback)
        RewardedAd.load(activity, realUnitId, AdUtils.getAdRequest(), object : RewardedAdLoadCallback() {
                override fun onAdFailedToLoad(p0: LoadAdError) {
                    super.onAdFailedToLoad(p0)
                    if (isTimeout) {
                        destroy()
                        return
                    }
                    sdkLoadFinish()
                    val code = getAdmobErrorCode(p0)
                    analysisErrorCode(adInfo, code)
                    callback.onAdLoadFailed(code)
                    destroy()
                }

                override fun onAdLoaded(p0: RewardedAd) {
                    super.onAdLoaded(p0)
                    if (isTimeout) {
                        destroy()
                        return
                    }
                    setAdLoadSuccess()
                    sdkLoadFinish()
                    this@AdmobRewardedAdUtils.rewardedAd = p0
                    p0.fullScreenContentCallback = object : FullScreenContentCallback(){

                        override fun onAdDismissedFullScreenContent() {
                            super.onAdDismissedFullScreenContent()
                            this@AdmobRewardedAdUtils.rewardedAd = null
                            setAdLoadShowing(false)
                            callback.onAdComplete()
                        }

                        override fun onAdFailedToShowFullScreenContent(p0: AdError) {
                            super.onAdFailedToShowFullScreenContent(p0)
                            val code = p0.code
                            setAdLoadShowing(false)
                            callback.onAdLoadFailed(code)
                            analysisErrorCode(adInfo, code)
                            sdkLoadFinish()
                            destroy()
                        }

                        override fun onAdShowedFullScreenContent() {
                            super.onAdShowedFullScreenContent()
                            setAdLoadShowing(true)
                            callback.onAdOpened()
                        }

                    }
                    callback.onAdLoaded(p0)
                }
            }
        )
    }






//    override fun loadAd(activity: Activity?, adInfo: AdInfo, callback: OnLoadAdCallback) {
//        super.loadAd(activity, adInfo, callback)
//        RewardedAd.load(
//            AdUtils.appContext,
//            realUnitId,
//            AdUtils.getAdRequest(),
//            object : RewardedAdLoadCallback() {
//                override fun onAdFailedToLoad(p0: LoadAdError) {
//                    super.onAdFailedToLoad(p0)
//                    val code = getAdmobErrorCode(p0)
//                    Log.d(AdConstants.TAG, "AdmobRewardedAdUtils fail -> $code")
//                    analysisErrorCode(adInfo, code)
//                    sdkLoadFinish()
//                    mOnLoadAdCallback?.onAdLoadFailed(code)
//                    destroy()
//                }
//
//                override fun onAdLoaded(p0: RewardedAd) {
//                    super.onAdLoaded(p0)
//                    sdkLoadFinish()
//                    this@AdmobRewardedAdUtils.rewardedAd = p0
//                    p0.fullScreenContentCallback = object : FullScreenContentCallback(){
//
//                        override fun onAdDismissedFullScreenContent() {
//                            super.onAdDismissedFullScreenContent()
//                            mOnLoadAdCallback?.onAdComplete()
//                        }
//
//                        override fun onAdFailedToShowFullScreenContent(p0: AdError) {
//                            super.onAdFailedToShowFullScreenContent(p0)
//                            val code = p0.code
//                            mOnLoadAdCallback?.onAdLoadFailed(code)
//                            analysisErrorCode(adInfo, code)
//                            Log.d(AdConstants.TAG, "AdmobRewardedAdUtils fail -> $code")
//                            sdkLoadFinish()
//                            destroy()
//                        }
//
//                        override fun onAdShowedFullScreenContent() {
//                            super.onAdShowedFullScreenContent()
//                            mOnLoadAdCallback?.onAdOpened()
//                        }
//
//                    }
//                    mOnLoadAdCallback?.onAdLoaded(p0)
//                }
//            }
//        )
//    }

    override fun showAd(activity: Activity) {
        super.showAd(activity)
        if (rewardedAd == null) {
            mOnLoadAdCallback?.onAdLoadFailed(AdConstants.ERROR_CODE_AD_LOADED_CAN_NOT_SHOW)
            return
        }

        rewardedAd?.show(activity) {
            mOnLoadAdCallback?.onUserEarnedReward(it.amount)
        }
    }


    override fun destroy() {
        super.destroy()
        rewardedAd = null
    }
}